<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>添加申购单</title>
    <script type="text/javascript">
      Ext.onReady(function(){

        var categoryDS = new Ext.data.Store({
          proxy:new Ext.data.HttpProxy({url:'/category/getCategories'}),
          reader:new Ext.data.JsonReader({
          }, [
            {name:'asset_name'},
            {name:'id'}
          ])
        });

        var orderNumber = new Ext.form.NumberField({
          fieldLabel: '申购序号',
          allowBlank: false,
          blankText: "申购单位不能为空",
          name: 'order_number',
          anchor: '90%'
        });
        
        var purchaseUnit = new Ext.form.TextField({
          fieldLabel: '申购单位',
          allowBlank: false,
          blankText: "申购单位不能为空",
          name: 'purchase_unit',
          anchor: '90%',
          readOnly: true,
          value:"<%=session[:department_name]%>"
        });
        
        //申购时间
        var actionDate = new Ext.form.DateField({
          fieldLabel: '申购时间',
          name: 'action_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-18",
          allowBlank: true,
          blankText: "申请日期必须选择",
          emptyText: '请选择日期...',
          anchor: '90%'
        });


        var subDept = new Ext.form.TextField({
          fieldLabel: '实验室(科室)',
          name: 'sub_dept',
          allowBlank: false,
          anchor: '90%'
        });

        // simple array store
        var purchaseTypeStore = new Ext.data.ArrayStore({
          //fields: ['value', 'text'],
          fields: ['id','text'],
          data : purchaseTypeDataArray
        });

        var purchaseType = new Ext.form.ComboBox({
          fieldLabel: '申购单类型',
          store: purchaseTypeStore,
          valueField:'id',
          displayField:'text',
          typeAhead: true,
          mode: 'local',
          triggerAction: 'all',
          selectOnFocus: true,
          hiddenName:'purchase_type',
          emptyText:'请选择申购单类型...',
          allowBlank: false,
          listWidth: 120,
          anchor: '90%'
        });

        var category = new Ext.form.ComboBox({
          store: categoryDS,
          displayField:'asset_name',
          hiddenName:'category_id',
          valueField:'id',
          typeAhead: true,
          allowBlank:false,
          minChars:1,
          anchor: '95%',
          mode: 'remote',
          fieldLabel:'产品分类',
          forceSelection: true,
          triggerAction: 'all',
          emptyText:'输入关键字将自动检索',
          selectOnFocus:true
        });
        var manager = new Ext.form.TextField({
          fieldLabel: '管理人',
          name: 'manager',
          anchor: '90%',
          readOnly: true,
          value:'<%=session[:applicant_name]%>'
        });

        var fundingSource = new Ext.form.TextField({
          fieldLabel: '经费来源',
          name: 'funding_source',
          allowBlank: false,
          anchor: '90%'
        });

        var fundingAccount = new Ext.form.TextField({
          fieldLabel: '经费账号',
          name: 'funding_account',
          allowBlank:false,
          anchor: '90%'
        });

        var fundingNature = new Ext.form.TextField({
          fieldLabel: '经费性质',
          name: 'funding_nature',
          allowBlank:false,
          disabled: true,
          anchor: '90%'
        });

        var productName = new Ext.form.TextField({
          fieldLabel: '产品名称',
          name: 'product_name',
          allowBlank: false,
          anchor: '90%'
        });

        var serviceLift = new Ext.form.NumberField({
          fieldLabel: '需用时间',
          name: 'service_lift',
          allowBlank: false,
          allowDecimals: false,
          allowNegative: false,
          disabled: true,
          anchor: '90%'
        });


        var quantity = new Ext.form.NumberField({
          fieldLabel: '台件数',
          name: 'quantity',
          allowBlank: false,
          allowDecimals: false,
          allowNegative: false,
          anchor: '90%'
        });

        var brand = new Ext.form.TextField({
          fieldLabel: '品牌',
          name: 'brand',
          anchor: '90%'
        });

        var model = new Ext.form.TextField({
          fieldLabel: '型号',
          allowBlank: false,
          name: 'model',
          anchor: '90%'
        });

        var specification = new Ext.form.TextField({
          fieldLabel: '型号规格',
          name: 'specification',
          anchor: '95%'
        });

        var manufacturer = new Ext.form.TextField({
          fieldLabel: '生产单位',
          name: 'manufacturer',
          anchor: '90%'
        });

        var estimatedPrice = new Ext.form.NumberField({
          fieldLabel: '单价',
          name: 'estimated_price',
          allowBlank: false,
          allowDecimals: false,
          allowNegative: false,
          //maxValue: 50000,
          anchor: '90%'
        });
        
        var estimatedAmount = new Ext.form.NumberField({
          fieldLabel: '总额',
          name: 'estimated_amount',
          readOnly: true,
          allowBlank: false,
          allowNegative: false,
          //maxValue: 100000,
          anchor: '90%'
        });

        var vendorName = new Ext.form.TextField({
          fieldLabel: '供应商',
          name: 'vendor_name',
          allowBlank: false,
          anchor: '95%'
        });

        var phone = new Ext.form.TextField({
          fieldLabel: '申购人电话',
          name: 'phone',
          allowBlank: false,
          anchor: '90%'
        });

        var mobile = new Ext.form.TextField({
          fieldLabel: '申购人手机',
          name: 'mobile',
          allowBlank: false,
          anchor: '90%'
        });

        //计算总额
        estimatedPrice.on('blur',countEstimatedAmount);
        quantity.on('blur',countEstimatedAmount);

        function countEstimatedAmount(){
          estimatedAmount.setValue(quantity.getValue()*estimatedPrice.getValue());
        }
        
        var qualification = new Ext.form.TextArea({
          fieldLabel: '主要技术指标',
          name: 'qualification',
          height:80,
          anchor: '97%'
        });

        var serviceRequest = new Ext.form.TextArea({
          fieldLabel: '服务要求',
          name: 'service_request',
          height:80,
          anchor: '97%'
        });

        //供货时间
        var supplyDate = new Ext.form.DateField({
          fieldLabel: '供货时间',
          name: 'supply_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-18",
          allowBlank: false,
          blankText: "申请日期必须选择",
          emptyText: '请选择日期...',
          anchor: '95%'
        });

        var supplyAddress = new Ext.form.TextField({
          fieldLabel: '供货地点',
          name: 'supply_address',
          anchor: '96%'
        });
        
        var reason = new Ext.form.TextArea({
          fieldLabel: '申请理由及详细用途',
          allowBlank: false,
          name: 'reason',
          height:80,
          anchor: '97%'
        });

        var newPurchaseForm = new Ext.FormPanel({
          labelWidth: 75, // label settings here cascade unless overridden
          url:'/purchase/create',
          frame:true,
          labelAlign: 'right',
          buttonAlign: 'center',
          //title: '添加申购单',
          bodyStyle:'padding:5px 5px 0',
          width: 760,
          //height:300,
          items: [{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [purchaseUnit]
                }, {
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [subDept]
                }, {
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [manager]
                }, {
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [purchaseType]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [fundingSource]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [fundingAccount]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [fundingNature]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [serviceLift]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [productName]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [brand]
                },{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [specification]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [manufacturer]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [quantity]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [estimatedPrice]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [estimatedAmount]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [vendorName]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [phone]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [mobile]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: 1,
                  layout: 'form',
                  border: false,
                  items: [qualification]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: 1,
                  layout: 'form',
                  border: false,
                  items: [reason]
                }]
            }],
          buttons: [{
              id:'ok',
              text: '保存',
              handler: function(){
                // check form value
                if (newPurchaseForm.form.isValid()) {
                  Ext.getCmp('ok').setDisabled(true);
                  //if (estimatedPrice.getValue() <= 10000 && estimatedAmount.getValue() <= 50000){
                    newPurchaseForm.form.submit({
                      method: 'GET',
                      success: function(result, response){
                        if (response.result.info == "success") {
                          //添加成功后，隐藏窗口，并刷新Grid
                          newPurchaseWin.destroy();
                        }else {
                          Ext.MessageBox.alert('消息', response.result.info);
                          Ext.getCmp('ok').setDisabled(false);
                        }
                      },
                      failure: function(result, response){
                        Ext.MessageBox.alert('提示', result.responseText);
                        Ext.getCmp('ok').setDisabled(false);
                      }
                    });
                  //}else{
                  //  Ext.MessageBox.alert('提示', "申购单的单项价格不能超过1万，总价不允许超过5万元！");
                  //  Ext.getCmp('ok').setDisabled(false);
                  //}
                }
                else {
                  Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                }
              }
            },{
              text: '取消',
              handler: function(){
                newPurchaseWin.destroy();
              }
            }]
        });
        newPurchaseForm.render('new_purchase_form');
      })
    </script>
  </head>
  <body>
    <div id="new_purchase_form"></div>
  </body>
</html>
